package com.baway.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.baway.base.path.RouterPath
import com.tencent.mmkv.MMKV


/**
 * Author: Jason Zhang
 * Date: 2025/8/19
 * Desc: 页面路由Token拦截器
 */
@Interceptor(priority = 2, name = "路由Token拦截器")
class RouteTokenInterceptor: IInterceptor {

    private lateinit var mContent: Context

    //不做登录校验的页面
    private var unCheckLoginStatePages = arrayListOf(
        RouterPath.PATH_LOGIN_PAGE,
        RouterPath.PATH_MAIN_PAGE,
    )

    override fun init(context: Context?) {
        context?.let {
            mContent = it
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        //获取Token
        val loginToken = MMKV.defaultMMKV().decodeString("token", "")
        if (loginToken.isNullOrEmpty() && !unCheckLoginStatePages.contains(postcard.path)){
            postcard?.group = RouterPath.GROUP_LOGIN
            postcard?.path = RouterPath.PATH_LOGIN_PAGE
            postcard?.navigation()
        }else{
            callback?.onContinue(postcard)
        }
    }


}